package com.jiaoxiaobo.exchange.topic;

import com.jiaoxiaobo.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class TopicExchangeCustomer {

    private static final String EXCHANGE_NAME = "topicExchange-20240913";

    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName,EXCHANGE_NAME,"*.info");
        channel.queueBind(queueName,EXCHANGE_NAME,"#.debug");

        Consumer myconsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body)
                    throws IOException {
                System.out.println("========================");
                String routingKey = envelope.getRoutingKey();
                System.out.println("routingKey >"+routingKey);
                String contentType = properties.getContentType();
                System.out.println("contentType >"+contentType);
                long deliveryTag = envelope.getDeliveryTag();
                System.out.println("deliveryTag >"+deliveryTag);
                System.out.println("content:"+new String(body,"UTF-8"));
                // (process the message components here ...)
                //消息处理完后，进行答复。答复过的消息，服务器就不会再次转发。
                //没有答复过的消息，服务器会一直不停转发。
                //				 channel.basicAck(deliveryTag, false);
            }
        };
        channel.basicConsume(queueName,true, myconsumer);
    }
}
